1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.BiMap;
23  import com.google.common.collect.testing.features.CollectionFeature;
24  import com.google.common.testing.SerializableTester;
25  
26  import java.io.Serializable;
27  
28  /**
29   * Tests for the {@code inverse} view of a BiMap.
30   * 
31   * <p>This assumes that {@code bimap.inverse().inverse() == bimap}, which is not technically
32   * required but is fulfilled by all current implementations.
33   * 
34   * @author Louis Wasserman
35   */
36  @GwtCompatible(emulated = true)
37  public class BiMapInverseTester<K, V> extends AbstractBiMapTester<K, V> {
38  
39    public void testInverseSame() {
40      assertSame(getMap(), getMap().inverse().inverse());
41    }
42  
43    @CollectionFeature.Require(SERIALIZABLE)
44    public void testInverseSerialization() {
45      BiMapPair<K, V> pair = new BiMapPair<K, V>(getMap());
46      BiMapPair<K, V> copy = SerializableTester.reserialize(pair);
47      assertEquals(pair.forward, copy.forward);
48      assertEquals(pair.backward, copy.backward);
49      assertSame(copy.backward, copy.forward.inverse());
50      assertSame(copy.forward, copy.backward.inverse());
51    }
52  
53    private static class BiMapPair<K, V> implements Serializable {
54      final BiMap<K, V> forward;
55      final BiMap<V, K> backward;
56  
57      BiMapPair(BiMap<K, V> original) {
58        this.forward = original;
59        this.backward = original.inverse();
60      }
61  
62      private static final long serialVersionUID = 0;
63    }
64  
65  }
66